package com.hljx.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hljx.config.exception.DataException;
import com.hljx.dto.SysRoleMenuDTO;
import com.hljx.entity.SysRoleMenuRelation;
import com.hljx.mapper.sys.SysRoleMenuRelationMapper;
import com.hljx.service.SysRoleMenuRelationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 角色菜单关联表 服务实现类
 * </p>
 *
 * @author gx
 * @since 2023-09-25
 */
@Service
public class SysRoleMenuRelationServiceImpl extends ServiceImpl<SysRoleMenuRelationMapper, SysRoleMenuRelation> implements SysRoleMenuRelationService {

    @Override
    public Boolean saveOrUpdateNew(SysRoleMenuDTO dto) {
        remove(Wrappers.lambdaUpdate(SysRoleMenuRelation.class).eq(SysRoleMenuRelation::getRoleId, dto.getRoleId()));
        List<SysRoleMenuRelation> list = new ArrayList<>();
        List<Long> menuIdList = dto.getMenuIdList();
        if (CollectionUtil.isEmpty(menuIdList)) {
            throw new DataException("入参数据为空",201);
        }
        menuIdList.forEach(e -> list.add(SysRoleMenuRelation.builder()
                .menuId(e)
                .roleId(dto.getRoleId())
                .build()));
        return saveBatch(list);
    }
}
